package firbi.base;

import android.app.Instrumentation;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.TextView;
import firbi.base.util.Constants;

/**
 * This class performs unit testing on DestinationView, including the list and responses
 * to different intents.
 * @author Kathryn Winglee
 *
 */

public class DestinationView_Unit extends ActivityUnitTestCase<DestinationView> {
	private Intent mStartIntent;//intent passed to DestinationView
	private DestinationView mDestView;
	private String mRoute = "0001_12";
	private String mRouteNum = "12";
	private Instrumentation.ActivityMonitor mMonitorSearch;
	private Instrumentation.ActivityMonitor mMonitorFav;
	
	/**
	 * Construct the class
	 */
	public DestinationView_Unit() {
        super(DestinationView.class);
	}
	
	/**
	 * Sets up each unit test
	 */
	@Override
    protected void setUp() throws Exception {
        super.setUp();
        mStartIntent = new Intent(Intent.ACTION_MAIN);
    }
	
	/**
	 * Set mDestView to null before calling super.
	 */
	@Override
	protected void tearDown() throws Exception {
		mDestView = null;
		super.tearDown();
	}

	/**
	 * Tests adding to the list and clicking on a destination
	 * @throws Exception
	 */
	@SmallTest
	public void testList() throws Exception {
        mStartIntent.putExtra(Constants.sROUTE_KEY, mRoute);
        DestinationView.suppressDialog();
        startActivity(mStartIntent, null, null);
        while(DestinationView.isThreadDone());
		mDestView = this.getActivity();
		ListView destList = mDestView.getListView();
		assertNotNull("Have a list", destList);
		assertNotNull(mDestView);
		
		/* test view set up */
		TextView rnumber =(TextView) mDestView.findViewById(R.id.routeNumber);
		assertNotNull(rnumber);
		assertEquals("route should be 12", rnumber.getText(), mRouteNum);
		
		/* test add*/
		mDestView.add("University District");
		mDestView.add("Downtown");
		mDestView.refreshList();
		ListView list = mDestView.getListView();
		assertEquals("add failed", 2, list.getCount());
		
		/* test click */
		mDestView.onListItemClick(list, mDestView.getCurrentFocus(), 0, 0);
		assertTrue(isFinishCalled());
		mDestView.onListItemClick(list, mDestView.getCurrentFocus(), 1, 1);
		assertTrue(isFinishCalled());

	}

	/**
	 * Tests the case when no route is passed in the intent. All other tests
	 * have the route id passed in.
	 * 
	 * @throws Exception
	 */
	@SmallTest
	public void testNoRoute() throws Exception {
		DestinationView.suppressDialog();
        startActivity(mStartIntent, null, null);
		Thread.sleep(40);
        assertTrue(isFinishCalled());
	}

	/**
	 * Tests the case when an incorrect route id is passed in the intent. All other
	 * tests have a correct route id passed in.
	 * 
	 * @throws Exception
	 */
	@SmallTest
	public void testBadRoute() throws Exception {
		mStartIntent.putExtra(Constants.sROUTE_KEY, "abc");
		DestinationView.suppressDialog();
        startActivity(mStartIntent, null, null);
		Thread.sleep(40);
        assertTrue(isFinishCalled());
	}
	
	/**
	 * Tests that each item in the menu sends the correct intent.
	 * @throws InteruptedException from threading
	 */
	@SmallTest
	public void testMenu() throws Exception{
		mMonitorSearch = new Instrumentation.ActivityMonitor("firbi.base.Search", null, true);
		mMonitorFav = new Instrumentation.ActivityMonitor("firbi.base.Favorites", null, true);
		this.getInstrumentation().addMonitor(mMonitorSearch);
    	this.getInstrumentation().addMonitor(mMonitorFav);
		
		mStartIntent = new Intent(Intent.ACTION_MAIN);
        mStartIntent.putExtra(Constants.sROUTE_KEY, mRoute);
        startActivity(mStartIntent, null, null);
    	mDestView = this.getActivity();
    	while (DestinationView.isThreadDone());
    	assertNotNull(mDestView);
    	Thread.sleep(1000);
		
    	/* Test if menu goes to search (list view) */
		assertEquals("Has been to search", 0, mMonitorSearch.getHits());
    	sendKeys(KeyEvent.KEYCODE_MENU);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_UP);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_UP);
 	    sendKeys(KeyEvent.KEYCODE_ENTER);
    	Thread.sleep(3000);
 	    assertEquals("Went to search (map)", 0, mMonitorSearch.getHits());
		Log.i("route test", "testMenu after list search");
 	    
 	    /* Test if menu goes to search (map view) */
    	sendKeys(KeyEvent.KEYCODE_MENU);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_UP);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
 	    sendKeys(KeyEvent.KEYCODE_ENTER);
    	Thread.sleep(3000);
 	    assertEquals("Went to search (map)", 0, mMonitorSearch.getHits());
 	    
 	   /* Test if menu goes to favorites*/
 	    assertEquals("Has been to favorites", 0, mMonitorFav.getHits());
    	sendKeys(KeyEvent.KEYCODE_MENU);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_UP);
 	    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
 	    sendKeys(KeyEvent.KEYCODE_ENTER);
    	Thread.sleep(3000);
    	assertEquals("Went to favorites", 0, mMonitorFav.getHits());
    }
}
